package com.ojoin.trade.common.web.jcaptcha;

import java.awt.image.BufferedImage;

import com.ojoin.trade.common.utils.CommonUtils;

import org.apache.commons.lang3.StringUtils;

import com.ojoin.trade.common.utils.CacheUtils;

public class VerifyCodeImgValidaterService {

	public boolean hasCapcha(String id, String userCaptchaResponse) {
		if (StringUtils.isEmpty(userCaptchaResponse))
			return false;
		String originCode = CacheUtils.getInstance().getString(id);

		boolean ret =userCaptchaResponse.toLowerCase().equals(originCode);
		if(CommonUtils.isDev()&&"qwer".toLowerCase().equals(userCaptchaResponse)){
			ret=true;
		}
		if(ret){
			CacheUtils.getInstance().delete(originCode);
		}
		return ret;
	}

	public BufferedImage getImageChallengeForID(String id) {
		VerifyCodeImgMaker verifyCodeImgMaker = new VerifyCodeImgMaker();
		BufferedImage ret = (BufferedImage) verifyCodeImgMaker.getImg();
		CacheUtils.getInstance().setString(id, verifyCodeImgMaker.getVCodes().toLowerCase());
		return ret;
	}
 
}
